﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using SilentWebsite.Model;
using SilentWebsite.Model.Contracts;
using SilentWebsite.ViewModels.Commands;
using SilentWebsite.ViewModels.Helpers;
using SilentWebsite.Views.Windows;
using NavigationService = SilentWebsite.ViewModels.Helpers.NavigationService;

namespace SilentWebsite.ViewModels
{
    public class MainPageViewModel : ViewModelBase
    {
        public ICommand Navigate { get; set; }
        public ICommand NavigateToProfile { get; set; }
        public ICommand Back { get; set; }
        public ICommand SignIn { get; set; }
        public ICommand SignOut { get; set; }
        public ICommand Register { get; set; }

        private bool signedInVisibility;
        private bool signedOutVisibility;
        private bool menuVisibility;
        private string username;
        private BitmapImage DefaultAvatar
        {
            get { return new BitmapImage(new Uri("/Resources/user.png", UriKind.Relative)); }
        }

        public ICollection<string> MenuItems { get; set; }
        public bool MenuVisibility
        {
            get { return menuVisibility; }
            set
            {
                menuVisibility = value;
                OnPropertyChanged("MenuVisibility");
            }
        }
        public bool SignedInVisibility
        {
            get { return signedInVisibility; }
            set
            {
                signedInVisibility = value;
                OnPropertyChanged("SignedInVisibility");
            }
        }
        public bool SignedOutVisibility
        {
            get { return signedOutVisibility; }
            set
            {
                signedOutVisibility = value;
                OnPropertyChanged("SignedOutVisibility");
            }
        }
        public string Username
        {
            get { return username; }
            set
            {
                username = value;
                OnPropertyChanged("Username");
            }
        }
        public BitmapImage Avatar { get; set; }

        public MainPageViewModel(Frame frame) : base("Main Page")
        {
            Navigate = new NavigateCommand { Type = NavigationPageType.SilverlightPage };
            NavigateToProfile = new SimpleCommand(() => NavigationService.Navigate("profile"));
            Back = new SimpleCommand(NavigationService.Back);
            SignIn = new SimpleCommand(() => new SignInChild().Show());
            SignOut = new SimpleCommand(() => SilentEngine.Instance.Authentication.SignOut());
            Register = new SimpleCommand(() => new RegisterChild().Show());

            MenuItems = new List<string> { "where to start", "design", "profile", "pages", "settings" };
            SignedInVisibility = true;
            SignedOutVisibility = false;
            MenuVisibility = false;
            Avatar = DefaultAvatar;

            NavigationService.Frame = frame;
            SilentEngine.Instance.Authentication.StatusChanged += AuthenticationOnStatusChanged;
        }

        private void AuthenticationOnStatusChanged(object sender, AuthenticationEventArgs e)
        {
            if(e.CurrentUser!= null)
                Username = e.CurrentUser.UserName;
            SilentEngine.Instance.CurrentUser = e.CurrentUser;
            NavigationService.Frame.JournalOwnership = JournalOwnership.OwnsJournal;
            NavigationService.Navigate(e.IsSignedIn ? "where to start" : "start");
            NavigationService.Frame.JournalOwnership = JournalOwnership.Automatic;
            MenuVisibility = e.IsSignedIn;
            SignedInVisibility = !e.IsSignedIn;
            SignedOutVisibility = e.IsSignedIn;
        }
    }
}
